/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.service.impl;

import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.anniversary.domain.AssociationUserInfo;
import me.zhengjie.modules.anniversary.domain.Photos;
import me.zhengjie.modules.anniversary.repository.AssociationUserInfoRepository;
import me.zhengjie.modules.anniversary.service.AssociationUserInfoService;
import me.zhengjie.modules.anniversary.service.dto.AssociationUserInfoDto;
import me.zhengjie.modules.anniversary.service.dto.AssociationUserInfoQueryCriteria;
import me.zhengjie.modules.anniversary.service.mapstruct.AssociationUserInfoMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author Jiayin Wei
* @date 2020-07-27
**/
@Service
@RequiredArgsConstructor
public class AssociationUserInfoServiceImpl implements AssociationUserInfoService {

    private final AssociationUserInfoRepository associationUserInfoRepository;
    private final AssociationUserInfoMapper associationUserInfoMapper;

    @Override
    public Map<String,Object> queryAll(AssociationUserInfoQueryCriteria criteria, Pageable pageable){
        Page<AssociationUserInfo> page = associationUserInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(associationUserInfoMapper::toDto));
    }

    @Override
    public List<AssociationUserInfoDto> queryAll(AssociationUserInfoQueryCriteria criteria){
        return associationUserInfoMapper.toDto(associationUserInfoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AssociationUserInfoDto findById(Long id) {
        AssociationUserInfo associationUserInfo = associationUserInfoRepository.findById(id).orElseGet(AssociationUserInfo::new);
        ValidationUtil.isNull(associationUserInfo.getId(),"AssociationUserInfo","id",id);
        return associationUserInfoMapper.toDto(associationUserInfo);
    }

    /*根据userId查询通讯录信息*/
    @Override
    public List<Map<String,Object>> getAssociationsByUserId(Long userId,Pageable pageable) {
        return associationUserInfoRepository.getAssociationsByUserId(userId,pageable);
    }

    /*根据userId和type查询通讯录信息*/
    @Override
    public List<Map<String,Object>> getAssociationsByType(Long userId, Integer type, Pageable pageable) {
        return associationUserInfoRepository.getAssociationsByType(userId,type,pageable);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AssociationUserInfoDto create(AssociationUserInfo resources) {
        return associationUserInfoMapper.toDto(associationUserInfoRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AssociationUserInfo resources) {
        AssociationUserInfo associationUserInfo = associationUserInfoRepository.findById(resources.getId()).orElseGet(AssociationUserInfo::new);
        ValidationUtil.isNull( associationUserInfo.getId(),"AssociationUserInfo","id",resources.getId());
        associationUserInfo.copy(resources);
        associationUserInfoRepository.save(associationUserInfo);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            associationUserInfoRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AssociationUserInfoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AssociationUserInfoDto associationUserInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", associationUserInfo.getName());
            map.put("校友会ID", associationUserInfo.getAssociationId());
            map.put("人员ID", associationUserInfo.getUserId());
            map.put("人员类型（0：教职工校友，1：学生校友）", associationUserInfo.getType());
            map.put("创建者名称", associationUserInfo.getCreateBy());
            map.put("创建时间", associationUserInfo.getCreateTime());
            map.put("修改者名称", associationUserInfo.getUpdatedBy());
            map.put("修改时间", associationUserInfo.getUpdateTime());
            map.put("排序", associationUserInfo.getSort());
            map.put("备注", associationUserInfo.getRemark());
            map.put("是否启用", associationUserInfo.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public List<Map<String,Object>> queryByName(String name,Pageable pageable) {
        name = '%'+name+'%';
        return associationUserInfoRepository.queryByName(name,pageable);
    }

    @Override
    public List<Map<String, Object>> queryByUserId(Long userId, Long type) {
        if (type==1){
            System.out.println("学生校友");
            return associationUserInfoRepository.queryStudentByUserId(userId);
        }else {
            System.out.println("教职工校友");
            return associationUserInfoRepository.queryTeacherByUserId(userId);
        }
    }

    @Override
    public List<Map<String,Object>>  queryByUseridStuDetail(Long id) {
        return associationUserInfoRepository.queryByUseridStuDetail(id);
    }
    @Override
    public List<Map<String,Object>> queryByUseridTeaDetail(Long id) {
        return associationUserInfoRepository.queryByUseridTeaDetail(id);
    }

    @Override
    public List<Map<String,Object>> queryByAssociationId(Long id,String userName,Pageable pageable) {
        return associationUserInfoRepository.queryByAssociationId(id,userName,pageable);
    }

    @Override
    public AssociationUserInfo queryByIdAndUserId(Long id, Long userId) {
        return associationUserInfoRepository.queryByIdAndUserId(id, userId);
    }

    @Override
    public void deleteUserInfoById(Long id) {
        associationUserInfoRepository.deleteUserInfoById(id);
    }

    @Override
    public List<Map<String, Object>> queryPhotoByAssId(Long id) {
        return associationUserInfoRepository.queryPhotoByAssId(id);
    }

    @Override
    public int insertPhotos(Photos photos){
        return associationUserInfoRepository.insertPhotos(photos);
    }
}
